home *** CD-ROM | disk | FTP | other *** search
- Path: news.mty.itesm.mx!academ07!al522331
- From: al522331@academ07.mty.itesm.mx (RAFAEL ALGARA TORRE)
- Newsgroups: comp.lang.c++
- Subject: Re: easy c++ question
- Date: 12 Apr 1996 16:33:37 GMT
- Organization: ITESM Campus Monterrey . DINF-DTCI
- Message-ID: <4km0l1$kiu@news.mty.itesm.mx>
- References: <316901DA.3138C677@ablecom.net>
- NNTP-Posting-Host: academ07.mty.itesm.mx
- X-Newsreader: TIN [version 1.2 PL2]
-
- The Letter O (jczech@ablecom.net) wrote:
- : Okay, I'm a newbie in C++, and am currently working through
- : Practical C++ programming by O'Reilly & Associates. I'm working
- : on my first C++ program with classes, and the objective is to
- : make a simple stack class with member functions, a constructor
- : and a destructor. simple right?
-
- : //======================================================
- : class stack {
- : private:
- : int count; // number of items in the stack
- : int data[100]; // the items themselves
- : public:
- : .
- : .
- : .
- : //======================================================
-
-
- : I was using the following init() member function to initialize
- : my stack variables:
-
- : //=======================================
- : inline void stack::init(void)
- : {
- : count = 0; // zero the stack
- : }
- : //=======================================
-
- : Simple, right? right. Then I modified it and made it into a
- : constructor, so that it would automatically be called whenever
- : I declared a variable of type: stack as follows:
-
- : //==============================================
- : inline stack::stack(void)
- : {
- : count = 0; // zero the stack
- : cout << "constructor has been called\n";
- : }
- : //==============================================
-
- : When I changed it to the constructor (above) it was not called
- : automatically when I declared a variable of type: stack, and
- : thus the stack counter wasn't initialized, and I got a SIGSEGV
- : signal because count starts out with a junk value that's beyond
- : the bounds of the data array of my stack variable.
-
- : My point is: Aren't constructors automagically called when a variable
- : is declared, simply because they have the same name as the class?
- : ie: stack::stack(void)
-
-
- : I'm using G++ 2.7.0 under Linux slackware 3.0, and Kernel 1.2.13
- : Is there some compiler switch to enable constructors? Is there a bug in
- : this version of g++? Am I just a moron who should be banished from programming?
-
- : Thanks for any help provided...
-
- : *Jczech@ablecom.net
-
- : Replies by email and/or newsgroup are appreciated.
-
-
- --
- I haven't ever worked on the platform you do I use BC++. But why do you write inline
- constructors anyway? A far as I understand, polymorphic objects resolve their
- construction sequence at runtime, so their constructors can't be inline, or at least, there's no reason to write them as inline.
-
-
- ------------------------------------------
- Rafael Algara Torre
- ITESM Monterrey, Mexico
- al522331@academ01.mty.itesm.mx
- ------------------------------------------
-